package games.padinvaders;


import hardware.Display2D;
import hardware.UI;

public class Defender extends SpaceShip{

	
//public static final int 	
	
public static final short color = 0x0F0; 	

public static final int SHIELD_MAX= 20;

public static final int ENERGY_MAX=300;



private int xVector; 
private int movieCounter;
private int moviePenality=8;


public static byte accXRef =0;




private boolean generator;

/**
 * 
 * 
 * 
 */

public void init(int energy, int shootCost, int lifePoints)
{
	movieCounter=0;
	xPos=4;
	yPos=0;
	status=STATUS_ALIVE;
	
	this.energy=energy;
	this.shootCost=shootCost;
	this.lifePoints=lifePoints;
	accXRef=UI.getAccAxis(UI.ACC_AXIS_X);
}


/**
 * 
 * 
 * 
 */


public void draw()
{
	Display2D.setPixel(xPos, yPos, color);
	{
		
		switch(status)
			{
			case STATUS_ALIVE:
					Display2D.setPixel((byte)xPos,(byte) yPos, color);
				break;
				
			case STATUS_EXPLODING_A:
				explodeA();
			break;
				

			case STATUS_KILLED:
				
			break;

				
			}

		displayStatus();

	}	
}




public void displayStatus()
{
	int counter;
	
 
	for (counter=0; counter<6; counter++)
		{
		if ((energy/shootCost)>counter)
			Display2D.setTopLED(counter, (short)0x40);
		}
	
	

	
	for (counter=0; counter<4; counter++)
		{
		if ((lifePoints/5)>counter)
				Display2D.setTopLED(11-counter, (short) 0xf);
		
		}

	
	boolean shield=false;
	
	counter=0;
	
	
	if (shield)
		{
		
		counter++;
		}
	
	
}




/**
 * 
 * 
 * 
 */

public void move()
{
	if (movieCounter==moviePenality)
		{
		xPos+=xVector;
		xVector=0;
		movieCounter=0;
		}
	else
	{
		movieCounter++;
	}
	
}


public void hit()
{
	
}



/**
 * 
 * 
 * 
 */

public void setMoveVector()
{
	
	if ((hardware.UI.keyLRight() & (xPos<(Display2D.getColumnCount()-1))))
			{
			xVector=1;
			}

		else if (hardware.UI.keyLLeft() & (xPos>0))
			{
			xVector=-1;					
			}
	
	
	//if (useACC)
	
	int accX;
	
	
	accX=UI.getAccAxis(UI.ACC_AXIS_X);
	
	
	if ((accX>(accXRef+10))  & (xPos>0))
		{
		xVector=-1;
		}
	else if((accX<(accXRef-10) & (xPos<(Display2D.getColumnCount()-1))))
		{
		xVector=1;
		}

	
	
	
	//TODO ACC
}



public void damage(InvaderProjectile myInvaderProjectile)
{

	lifePoints-=myInvaderProjectile.doDamage(xPos,yPos);
	

	if (lifePoints<=0)
		{
		status=STATUS_EXPLODING_A;
		animationCounter=0;
		}
}





public void damage(int damage)
{

	lifePoints-=damage;
	

	if (lifePoints<=0)
		{
		status=STATUS_EXPLODING_A;
		animationCounter=0;
		}
}




public void charge(int energy)
{
	if (generator)
		{
		energy*=2;
		}
	
	
	if ((shield) && (shieldStrength<SHIELD_MAX)) 
		{
		shieldStrength++;
		energy=energy-1;
		}

	if(this.energy<ENERGY_MAX)
		this.energy+=energy;
	
}

/**
 * 
 * @return
 */

public boolean shoot(DefenderProjectile myDefenderProjectile,int projectileType)
{
	if (energy>=shootCost)
		{
		energy-=shootCost;
	
		myDefenderProjectile.fire(xPos,projectileType);
		return(true);
		}
	
	
	return(false);
}


}
